package org.linlinjava.litemall.admin.service;

import org.linlinjava.litemall.admin.dao.AdminToken;
import org.linlinjava.litemall.core.util.CharUtil;

import java.io.*;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class AdminTokenManager implements Serializable{
    private static Map<String, AdminToken> tokenMap = new HashMap<>();
    private static Map<Integer, AdminToken> idMap = new HashMap<>();

    public static Integer getUserId(String token) {

        if (tokenMap.isEmpty() && idMap.isEmpty()){
            Map<String, Object> o = deserialize();
            if (o != null) {
                tokenMap = (Map<String, AdminToken>)o.get("tokenMap");
                idMap = (Map<Integer, AdminToken>)o.get("idMap");
            }
        }

        AdminToken userToken = tokenMap.get(token);
        if(userToken == null){
            return null;
        }

        if(userToken.getExpireTime().isBefore(LocalDateTime.now())){
            tokenMap.remove(token);
            idMap.remove(userToken.getUserId());
            return null;
        }

        return userToken.getUserId();
    }


    public static AdminToken generateToken(Integer id){
        AdminToken userToken = null;

//        userToken = idMap.get(id);
//        if(userToken != null) {
//            tokenMap.remove(userToken.getToken());
//            idMap.remove(id);
//        }

        if (tokenMap.isEmpty() && idMap.isEmpty()){
            Map<String, Object> o = deserialize();
            if (o != null) {
                tokenMap = (Map<String, AdminToken>)o.get("tokenMap");
                idMap = (Map<Integer, AdminToken>)o.get("idMap");
            }
        }


        String token = CharUtil.getRandomString(32);
        while (tokenMap.containsKey(token)) {
            token = CharUtil.getRandomString(32);
        }

        LocalDateTime update = LocalDateTime.now();
        LocalDateTime expire = update.plusDays(7);

        userToken = new AdminToken();
        userToken.setToken(token);
        userToken.setUpdateTime(update);
        userToken.setExpireTime(expire);
        userToken.setUserId(id);
        tokenMap.put(token, userToken);
        idMap.put(id, userToken);

        Map<String,Object> map = new HashMap<>();
        map.put("tokenMap",tokenMap);
        map.put("idMap",idMap);
        serialize(map);

        return userToken;
    }

    public static void serialize(Object o){
        ObjectOutputStream oo = null;
        try {
            oo = new ObjectOutputStream(new FileOutputStream(
                            new File("D:/obj.txt")));
            oo.writeObject(o);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (oo != null){
                try {
                    oo.flush();
                    oo.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static Map<String,Object> deserialize(){
        ObjectInputStream ois =null;
        try {
            File file = new File("D:/obj.txt");
            if(file.exists()){
                ois = new ObjectInputStream(new FileInputStream(
                        file));
                return  (Map<String,Object> )ois.readObject();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (ois != null){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

}
